import 'package:flutter/material.dart';
import 'package:safemap/safemap.dart';

// 错误类型
enum VehicleErrorType {
  // 保护型错误
  protected(1),
  // 警告错误
  warning(2),
  // 错误
  error(3);

  final int value;
  const VehicleErrorType(this.value);

  Color get color {
    switch (this) {
      case VehicleErrorType.error:
        return Colors.red;
      case VehicleErrorType.warning:
        return Colors.orange;
      case VehicleErrorType.protected:
        return Colors.white;
      default:
        return Colors.grey;
    }
  }
}

// 车辆错误
class VehicleErrorModel {
  VehicleErrorType? type; // 错误类型
  String? message; // 错误信息

  VehicleErrorModel({
    this.type,
    this.message,
  });

  factory VehicleErrorModel.fromMap(Map<String, dynamic>? data) {
    if (data == null) return VehicleErrorModel();
    SafeMap safeMap = SafeMap(data);
    VehicleErrorModel model = VehicleErrorModel();
    model.type = VehicleErrorType.values[safeMap['type'].toInt ?? 5];
    model.message = safeMap['message'].string;
    return model;
  }

  Map<String, dynamic> toMap() {
    return {
      'type': type?.value ?? 5,
      'message': message ?? "",
    };
  }
}
